package editor.modelo;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Polygon;
import java.awt.Rectangle;
import java.awt.Shape;
import java.awt.geom.AffineTransform;
import java.awt.geom.PathIterator;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.util.ArrayList;
import java.util.List;

import editor.graphics.Dibujable;

public class EditorSpace extends Space implements Dibujable{
	private static final int TAMANIO_SPACES = 8;
	private static final Color COLOR_SPACES = Color.BLUE;
	private static final Color COLOR_PUNTOS = new Color(80,80,120, 180);
	private static final int TAMANIO_PUNTOS = 4;
	private static final Color COLOR_EDGES = new Color(15,150,15);

	private static final Color COLOR_SPACES_SELECCIONADO = new Color(150,255,150, 150);
	private static final int TAMANIO_SPACE_SELECCIONADO = 16;
	private static final int TAMANIO_PUNTOS_SELECCIONADO = 6;
	private static final Color COLOR_UNION_SPACE_PTO = Color.RED;
	private static final Color COLOR_PUNTOS_SELECCIONADO = new Color(150,200,200, 180);


	public EditorSpace(){
		this(null, false);
	}
	public EditorSpace(Point2D centro, boolean con4Puntos){
		super();
		this.tipo= Tipo.values()[0];
		this.name= "Nuevo";
		if(con4Puntos){
			this.centro = centro;
			int a = -TAMANIO_SPACES*5;
			int b = -TAMANIO_SPACES*5;
			this.borde.add(new Point2D.Float((float)centro.getX() + a, (float)centro.getY()+ b));
			a = TAMANIO_SPACES*5;
			this.borde.add(new Point2D.Float((float)centro.getX() + a, (float)centro.getY()+ b));
			b = TAMANIO_SPACES*5;
			this.borde.add(new Point2D.Float((float)centro.getX() + a, (float)centro.getY()+ b));
			a = -TAMANIO_SPACES*5;
			this.borde.add(new Point2D.Float((float)centro.getX() + a, (float)centro.getY()+ b));

		}
	}

	public void insertPointAfter(Point2D nuevo, Point2D despuesDe){
		int i = borde.indexOf(despuesDe);
		if(i>=0){
			borde.add(i+1, nuevo);
		}else
			borde.add(nuevo);
	}



	public void dibujar(Graphics2D g) {
		g.setColor(COLOR_SPACES);	 //spaces

		if(this.name != null){
			g.drawString(this.name + " (" +String.valueOf(this.height)+")",
					(int)centro.getX()-5 ,(int)centro.getY()-10);
		}
		if(this.tipo != null){
			g.drawString(this.tipo.toString(),
					(int)centro.getX()-5 ,(int)centro.getY()+20);
		}
		g.fillOval((int)centro.getX()-(TAMANIO_SPACES>>1),
				(int)centro.getY()-(TAMANIO_SPACES>>1),
				TAMANIO_SPACES, TAMANIO_SPACES);

		g.setColor(COLOR_PUNTOS);
		for(Point2D p: borde){
			g.fillRect((int)p.getX() - (TAMANIO_PUNTOS>>1), (int)p.getY() - (TAMANIO_PUNTOS>>1), 
					TAMANIO_PUNTOS , TAMANIO_PUNTOS);
		}
		g.setColor(COLOR_EDGES);
		if(borde.size() > 0 ){
			for(int i = 0; i<borde.size()-1; i++){
				g.drawLine((int)borde.get(i).getX(),
						(int)borde.get(i).getY(),
						(int)borde.get(i+1).getX(),
						(int)borde.get(i+1).getY());
			}
			g.drawLine((int)borde.get(0).getX(),
					(int)borde.get(0).getY(),
					(int)borde.get(borde.size()-1).getX(),
					(int)borde.get(borde.size()-1).getY());
		}



	}

	public void deletePoint(Point2D punto) {
		this.borde.remove(punto);

	}


	public void dibujarSelccionado(Graphics2D g) {
		g.setColor(COLOR_SPACES_SELECCIONADO);	 //spaces
		g.fillOval((int)centro.getX()-(TAMANIO_SPACE_SELECCIONADO>>1), 
				(int)centro.getY()-(TAMANIO_SPACE_SELECCIONADO>>1), 
				TAMANIO_SPACE_SELECCIONADO, TAMANIO_SPACE_SELECCIONADO);

		g.setColor(COLOR_UNION_SPACE_PTO);	 //spaces
		for(Point2D pto: borde){
			g.drawLine((int)centro.getX(), (int)centro.getY(),
					(int)pto.getX(), (int)pto.getY()
					);

		}

		g.setColor(COLOR_EDGES);
		if(this.name != null){
			g.drawString(this.name + " (" +String.valueOf(this.height)+")",(int)centro.getX()-5 ,(int)centro.getY()-10);
		}
		//	g.setColor(COLOR_EDGES);
		if(this.tipo != null){
			g.drawString(this.getTipo().toString(), (int)centro.getX()-5 ,(int)centro.getY()+20);
		}
		if(borde.size() > 0 ){
			for(int i = 0; i<borde.size()-1; i++){
				g.drawLine((int)borde.get(i).getX(),
						(int)borde.get(i).getY(),
						(int)borde.get(i+1).getX(),
						(int)borde.get(i+1).getY());
			}
			g.drawLine((int)borde.get(0).getX(),
					(int)borde.get(0).getY(),
					(int)borde.get(borde.size()-1).getX(),
					(int)borde.get(borde.size()-1).getY());
		}

		g.setColor(COLOR_PUNTOS_SELECCIONADO);
		for(Point2D p: borde){
			g.fillRect((int)p.getX() - (TAMANIO_PUNTOS_SELECCIONADO>>1), (int)p.getY() - (TAMANIO_PUNTOS_SELECCIONADO>>1), 
					TAMANIO_PUNTOS_SELECCIONADO , TAMANIO_PUNTOS_SELECCIONADO);
		}

	}

	public void move(int offX, int offY){
		for (Point2D p : this.borde) {
			p.setLocation(p.getX()+offX, p.getY()+offY);

		}
		this.centro.setLocation(centro.getX()+offX, centro.getY()+offY);
	}
	public EditorSpace cloneTo(int xMouse, int yMouse) {
		int difX = (int) -this.getCentro().getX() + xMouse;
		int difY = (int) -this.getCentro().getY() + yMouse;
		EditorSpace es = new EditorSpace();
		es.setTipo(this.getTipo());
		//es.setNombrePadreCompleto(this.no)
		es.setName(this.name+"I");
		es.setCentro(xMouse, yMouse);
		for(Point2D p : getBorde()){
			Point2D.Float f = new Point2D.Float((float)p.getX()+difX, (float)p.getY()+difY);
			es.borde.add(f);
		}
		return es;
	}

	public void alignBorder(int dif) {
		Point2D act, ant;
		for(int i = 1; i< borde.size(); i++){
			act = borde.get(i);
			ant = borde.get(i-1);
			if(Math.abs(act.getX() - ant.getX()) <= dif){
				if(Math.abs(act.getY() - ant.getY()) >= dif){ //que no se peguen los puntos
					act.setLocation(ant.getX(), act.getY());
				}
			}
			if(Math.abs(act.getY() - ant.getY()) <= dif){
				if(Math.abs(act.getX() - ant.getX()) >= dif){
					act.setLocation(act.getX(), ant.getY());
				}
			}
		}
		act = borde.get(borde.size()-1);
		ant = borde.get(0);
		if(Math.abs(act.getX() - ant.getX()) <= dif){
			if(Math.abs(act.getY() - ant.getY()) >= dif){
				act.setLocation(ant.getX(), act.getY());
			}
		}
		if(Math.abs(act.getY() - ant.getY()) <= dif){
			if(Math.abs(act.getX() - ant.getX()) >= dif){
				act.setLocation(act.getX(), ant.getY());
			}
		}
	}


	/*
	public static void main (String[] args){
		int[] ar = new int[]{1,2,3,4,5};
		ArrayList a = new ArrayList();
		for(int i =0; i<ar.length; i++){
			a.add(ar[i]);
		}
		invertPoints(a);
		for(int i = 0; i<ar.length; i++){
			System.out.println(a.get(i));
		}

	}*/




}
